package cn.tedu.egarticle.base.exception;

import cn.tedu.egarticle.base.response.JsonResult;
import cn.tedu.egarticle.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        log.debug("doHandleMethodArgumentNotValidException = " + ex);
        String data = ex.getFieldError().getDefaultMessage();
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }
    @ExceptionHandler
    public JsonResult doHandleConstraintViolationException(ConstraintViolationException ex){
        log.debug("doHandleConstraintViolationException = " + ex);
        String data = ex.getMessage().split(":")[1].trim();
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }






















//    @ExceptionHandler
//    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
//        String data = ex.getFieldError().getDefaultMessage();
//        log.error("MethodArgumentNotValidException = " + data);
//        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
//    }
//    @ExceptionHandler
//    public JsonResult doHandle(ConstraintViolationException ex){
//        String data = ex.getMessage().split(":")[1].trim();
//        log.error("ConstraintViolationException = " + data);
//        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
//    }
}
